using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;

public partial class feeds : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["type"] == null)
            loadRSS();
        else
        {
            switch (Request.QueryString["type"].ToLower())
            {
                case "rss":
                    loadRSS();
                    break;
                case "atom":
                    break;
                default:
                    break;
            }
        }
    }

    private void loadRSS()
    {
        int intPages_Id = 3;
        string strConn = System.Configuration.ConfigurationManager.AppSettings["connectionString"];
        bool isError = false;

        MySqlConnection conn = new MySqlConnection(strConn);
        MySqlCommand comm = new MySqlCommand("SELECT * FROM Articles WHERE Pages_Id=" + intPages_Id, conn);
        MySqlDataAdapter da = new MySqlDataAdapter(comm);
        DataTable dt = new DataTable();

        try { da.Fill(dt); }
        catch
        { isError = true; }

        System.Text.StringBuilder sbFeed = new System.Text.StringBuilder();
        sbFeed.Append("<?xml version=\"1.0\"?>");
        // RSS Data
        sbFeed.Append("<rss version=\"2.0\">");
        // Channel Data
        sbFeed.Append("<channel>");
        sbFeed.Append("<title>thinking web 2.0</title>");
        sbFeed.Append("<link>http://www.manddar.com/default.aspx?id=3</link>");
        sbFeed.Append("<description>personal blog of Mandar Oak</description>");
        // Articles data
        if (!isError && dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                sbFeed.Append("<item>");
                sbFeed.Append("<title>" + dr["Title"].ToString().Trim() + "</title>");
                sbFeed.Append("<link>http://www.manddar.com/articles.aspx?id=" + dr["Articles_Id"].ToString() + "</link>");
                sbFeed.Append("<description><![CDATA[" + dr["Content"].ToString().Trim() + "]]></description>");
                sbFeed.Append("<pubDate>" + DateTime.Parse(dr["date_added"].ToString()).ToString("ddd, dd MMM yyyy HH:mm:ss zz00") + "</pubDate>");
                sbFeed.Append("<guid>http://www.manddar.com/articles.aspx?id=" + dr["Articles_Id"].ToString() + "</guid>");
                sbFeed.Append("</item>");
            }
        }
        // Articles Data end
        // Channel data end
        sbFeed.Append("</channel>");
        // RSS Data End
        sbFeed.Append("</rss>");

        Response.ClearContent();
        Response.ContentType = "text/xml";
        Response.Output.Write(sbFeed.ToString());
    }
}
